package com.xukang.qd.client;

import com.ibm.mq.*;
import com.ibm.msg.client.wmq.v6.base.internal.MQC;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 链接器操作接受
 */
public class ConsumerIbmMqUtil {

    public static Logger logger= LoggerFactory.getLogger(ConsumerIbmMqUtil.class);


    private  int reasonCode = 2033;
    private String queueName ;
    private String queueManagerName ;

    /**
     * 根据messageId(correlationId)调用读取请求消息队列方法，启动监听模式
     * @author xukang
     */
    public String retriRequest(String mesageId) {
        String result = "fail";
        MQQueue queue = null;
        MQQueueManager mqMgr= null;
        logger.info("根据messageId(correlationId)调用读取请求消息队列方法");
        try {
            mqMgr = new MQQueueManager(queueManagerName);
            /**
             * 关闭了就重新打开
             */
            if (mqMgr == null || !mqMgr.isConnected()) {
                mqMgr = new MQQueueManager(queueManagerName);
            }
            queue = mqMgr.accessQueue(queueName, MQC.MQGMO_WAIT);
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.options = gmo.options + MQC.MQGMO_WAIT;
            //  gmo.waitInterval = MQC.MQWI_UNLIMITED;
            // TODO: 2019/8/14   设置等待的毫秒时间限制
            gmo.waitInterval = 2000;
            MQMessage mqMessage = new MQMessage();
            // TODO: 2019/8/13 注意：messageId 会自动生成一个16位的随机字符，不可更改 
          /*  mqMessage.mesageId = mesageId.getBytes("gbk");*/
            queue.get(mqMessage, gmo);
            int length = mqMessage.getDataLength();
            byte[] msg = new byte[length];

            mqMessage.readFully(msg);
            result = new String(msg, "GBK");
            //第四步：保存messageId,并在发送消息的时候设置correlationId为该messageId
            logger.info("根据messageID获取信息结果：" + result);

        } catch (Exception e) {
            logger.error("mq队列queueManagerName："+queueManagerName+",queueName:"+queueName+"发生异常：{}",e);
        }finally {
            try {
                queue.close();
                //断开队列管理器的连接
                mqMgr.disconnect();
            } catch (MQException e1) {
                logger.error("关闭连接发生异常：{}",e1);
            }
        }
        return result;
    }
    /**
     * 根据messageId(correlationId)调用读取响应消息队列方法
     * @param messageId
     * @author xukang
     */
    public String retriResp(String messageId){
        logger.info("根据messageId(correlationId)调用读取响应消息队列方法");
        String result = "fail";
        MQQueue queue = null;
        MQQueueManager mqMgr= null;
        try {
            mqMgr = new MQQueueManager(queueManagerName);
            /**
             * 关闭了就重新打开
             */
            if (mqMgr == null || !mqMgr.isConnected()) {
                mqMgr = new MQQueueManager(queueManagerName);
            }
            queue = mqMgr.accessQueue(queueName, MQC.MQGMO_WAIT);
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.options = gmo.options + MQC.MQGMO_WAIT;
            //  gmo.waitInterval = MQC.MQWI_UNLIMITED;
            // 设置等待的毫秒时间限制
            gmo.waitInterval = 10000;
            MQMessage mqMessage = new MQMessage();
            // =Hex.decodeHex(messageID.toCharArray());
            mqMessage.correlationId =Hex.decodeHex(messageId.toCharArray());
            queue.get(mqMessage, gmo);
            byte[] mm = new byte[mqMessage.getDataLength()];
            mqMessage.readFully(mm);
            result = new String(mm, "GBK");
            //第四步：保存messageId,并在发送消息的时候设置correlationId为该messageId
            logger.info("根据messageID获取信息结果："+result);
        } catch (Exception e) {
            logger.error("mq队列queueManagerName："+queueManagerName+",queueName:"+queueName+"发生异常：{}",e);
        }finally {
            try {
                queue.close();
                //断开队列管理器的连接
                mqMgr.disconnect();
            } catch (MQException e1) {
                logger.error("关闭连接发生异常：{}",e1);
            }
        }
        return result;
    }

    public int getReasonCode() {
        return reasonCode;
    }

    public void setReasonCode(int reasonCode) {
        this.reasonCode = reasonCode;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getQueueManagerName() {
        return queueManagerName;
    }

    public void setQueueManagerName(String queueManagerName) {
        this.queueManagerName = queueManagerName;
    }

    public ConsumerIbmMqUtil() {
    }
    public  void init(){
        logger.info("初始化队列" + queueName);
    }

}
